package com.itboye.mengwu.app.ui.aizhiwu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.R.id;
import com.itboye.mengwu.data.model.AllData;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.QueryCollectionModel;
import com.itboye.mengwu.data.model.QueryCollectionModel.A;
import com.itboye.mengwu.data.presenter.TieZiPresenter;

/**
 * 首页ListView的数据适配器
 * 
 * @author Administrator
 * 
 */
public class ItemAdapter extends BaseAdapter implements Observer{

	private Context mContext;
	private QueryCollectionModel queryCollectionModel;
	TieZiPresenter tieZiPresenter;
	

	public ItemAdapter(Context ctx, QueryCollectionModel queryCollectionModel) {
		this.mContext = ctx;
		this.queryCollectionModel = queryCollectionModel;
		tieZiPresenter= new TieZiPresenter(this);
	}

	@Override
	public int getCount() {
		return queryCollectionModel == null ? 0 : queryCollectionModel.getList().size();
	}

	@Override
	public Object getItem(int position) {
		return queryCollectionModel.getList().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_countxt, null);
			holder.imgName = (ImageView) convertView.findViewById(R.id.imgName);
			holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
			holder.cou_content = (TextView) convertView.findViewById(R.id.cou_content);
			holder.gridview = (NoScrollGridView) convertView.findViewById(R.id.gridview);
			holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
			holder.txtSex = (TextView) convertView.findViewById(R.id.txtSex);
			holder.imageView2= (TextView) convertView.findViewById(R.id.imageView2);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		A itemEntity = queryCollectionModel.getList().get(position);

		holder.txtName.setText(itemEntity.getauthor());
		holder.cou_content.setText("【 "+itemEntity.getSubject() +" 】 "+itemEntity.getMessage());

		holder.textView2
				.setText(babybirth(queryCollectionModel.getList().get(position).getBabyDefault().getBirthday()));
		if ("1".equals(queryCollectionModel.getList().get(position).getBabyDefault().getSex())) {
			  holder.txtSex.setText("男宝");
		} else if ("0".equals(queryCollectionModel.getList().get(position).getBabyDefault().getSex())) {
			  holder.txtSex.setText("女宝");
		}

//		XImageLoader.load(SPUtils.get(mContext, null, SpPublicParams.USERHEAD, "") + "", holder.imgName);
		XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
				+itemEntity.getHead(), holder.imgName);
		
		final ArrayList<String> imageUrls = (ArrayList<String>) queryCollectionModel.getList().get(position).getImg();
		if (imageUrls == null || imageUrls.size() == 0) { //
			// 没有图片资源就隐藏GridView
			holder.gridview.setVisibility(View.GONE);
		} else {
			holder.gridview.setAdapter(new NoScrollGridAdapter(mContext, imageUrls));
		}
		// 点击回帖九宫格，查看大图
		holder.gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				imageBrower(position, imageUrls);
			}
		});
		holder.imageView2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				tieZiPresenter.favorite(queryCollectionModel.getList().get(position).getTid(), queryCollectionModel.getList().get(position).getUid(), "0");
				
			}
		});
		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}

	/**
	 * listview组件复用，防止“卡顿”
	 * 
	 * @author Administrator
	 * 
	 */
	class ViewHolder {
		private ImageView imgName;
		private TextView txtName,imageView2;
		private TextView cou_content, textView2, txtSex;
		private NoScrollGridView gridview;
	}

	String babybirth(String birth) {
		String babyyears;
		SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy年MM月dd日");
		Calendar calendar = Calendar.getInstance();
		int yearNow = calendar.get(Calendar.YEAR);
		int monthNow = calendar.get(Calendar.MONTH);
		int dayNow = calendar.get(Calendar.DAY_OF_MONTH);
		Log.d("hu-----", yearNow + "monthNow-----" + "-------dayBirth" + monthNow + "n" + dayNow);
		Calendar calendar2 = Calendar.getInstance();
		long timews = Long.valueOf(birth);
		Date date = new Date(timews * 1000L);
		calendar2.setTime(date);
		// Log.d("hu", date.toString());
		int yearBirth = calendar2.get(Calendar.YEAR);
		int monthBirth = calendar2.get(Calendar.MONTH);
		int dayBirth = calendar2.get(Calendar.DAY_OF_MONTH);
		Log.d("hu-----2", yearBirth + "monthBirth-----" + "-------dayBirth" + monthBirth + "n" + dayBirth);
		int age = yearNow - yearBirth;
		int month = monthNow - monthBirth;
		int day = dayNow - dayBirth;
		if (age > 0) {
			if (month > 0) {
				if (day < 0) {
					month--;
					age--;
				}

			} else if (month < 0) {
				month += 12;
				if (day < 0) {
					month--;
					age--;
				}
			}

		} else {
			if (day < 0) {
				month--;
			}

		}
		SimpleDateFormat myFmt5 = new SimpleDateFormat("d");
		String birthday2 = myFmt1.format(Long.parseLong(birth) * 1000L);
		String birthday = myFmt5.format(System.currentTimeMillis() - Long.parseLong((birth) + "") * 1000L);
		int days = Integer.parseInt(birthday) % 30;
		Log.d("hu-bir", birthday2);
		Log.d("hu-old", (age + "岁" + month + "月" + days + "天"));
		if (day == 0) {
			babyyears = age + "岁" + month + "月" + day + "天";
		} else {
			babyyears = age + "岁" + month + "月" + days + "天";
		}

		return babyyears;
	}

	@Override
	public void update(Observable observable, Object data) {
		
		
	}



}
